﻿namespace Rzrlf.SP.Innovator.WebParts.Utils
{
    internal class IomUtil
    {
        internal static bool CreatePartECO(string id, string description, string reason, string owner)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item relItem = invtr.newItem("Affected Item", "add");
            relItem.setProperty("action", "change");
            relItem.setProperty("interchangeable", "1");
            relItem.setProperty("affected_id", id);

            Aras.IOM.Item rel = invtr.newItem("Simple ECO Affected Item", "add");
            rel.setRelatedItem(relItem);

            Aras.IOM.Item mngr = invtr.newItem("Identity", "get");
            mngr.setProperty("name", "Change Specialist I");

            Aras.IOM.Item ownr = invtr.newItem("Identity", "get");
            ownr.setProperty("name", owner); // System.Security.Principal.WindowsIdentity identity = System.ServiceModel.ServiceSecurityContext.Current.WindowsIdentity;

            Aras.IOM.Item item = invtr.newItem("Simple ECO", "add");
            item.setProperty("description", description);
            item.setProperty("change_reason", reason);
            item.setPropertyItem("owned_by_id", ownr);
            item.setPropertyItem("managed_by_id", mngr);
            item.addRelationship(rel);

            Aras.IOM.Item result = item.apply();

            return true;
        }

        internal static string GetPart(string id)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item item = invtr.newItem("Part", "get");
            item.setID(id);

            Aras.IOM.Item result = item.apply();

            return result.ToString();
        }

        internal static string GetPartBOM(string id)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item rel = invtr.newItem("Part BOM", string.Empty);
            rel.setAttribute("repeatProp", "related_id");
            rel.setAttribute("repeatTimes", "0");

            Aras.IOM.Item item = invtr.newItem("Part", "GetItemRepeatConfig");
            item.setID(id);
            item.addRelationship(rel);

            Aras.IOM.Item result = item.apply();

            return result.ToString();
        }

        internal static string GetPartDocuments(string id)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item relItem = invtr.newItem("SPDoc", "get");

            Aras.IOM.Item rel = invtr.newItem("Part SPDoc", "get");
            rel.setAttribute("related_expand", "0");
            rel.setRelatedItem(relItem);

            Aras.IOM.Item item = invtr.newItem("Part", "get");
            item.setID(id);
            item.addRelationship(rel);

            Aras.IOM.Item result = item.apply();

            return result.ToString();
        }

        internal static string GetPartECOs(string id)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item item = invtr.newItem("Simple ECO", "get");
            item.setAttribute("where", string.Format("SIMPLE_ECO.id in (select source_id from SIMPLE_ECO_AFFECTED_ITEM A inner join AFFECTED_ITEM B on A.related_id = B.id where B.affected_id in (select id from PART where config_id = (select config_id from PART where id = '{0}')))", id));

            Aras.IOM.Item result = item.apply();

            return result.ToString();
        }

        internal static bool UpdatePart(string id, System.Collections.Generic.Dictionary<string, string> props, System.Collections.Generic.Dictionary<string, string> attrs)
        {
            Aras.IOM.Innovator invtr = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetIomInstance();

            Aras.IOM.Item item = invtr.newItem("Part", "EditReleasedPart");
            item.setID(id);

            foreach (string key in props.Keys)
            {
                item.setProperty(key, props[key]);
            }

            foreach (string key in attrs.Keys)
            {
                item.setAttribute(key, attrs[key]);
            }

            item.apply();

            return true;
        }

        #region Helper Methods...

        public static Aras.IOM.Innovator GetIomInstance()
        {
            Aras.IOM.Innovator invtr = Aras.IOM.IomFactory.CreateInnovator(null);

            //Aras.IOM.WinAuthHttpServerConnection con = Aras.IOM.IomFactory.CreateWinAuthHttpServerConnection(url, database);
            Aras.IOM.HttpServerConnection con = Aras.IOM.IomFactory.CreateHttpServerConnection(Rzrlf.SP.Innovator.WebParts.Settings.InnovatorServerUrl, Rzrlf.SP.Innovator.WebParts.Settings.InnovatorDatabaseName, Rzrlf.SP.Innovator.WebParts.Settings.InnovatorUserName, Rzrlf.SP.Innovator.WebParts.Settings.InnovatorPassword);

            Aras.IOM.Item result = con.Login();
            if (result.isError())
            {
                throw new System.Security.SecurityException(string.Format("The requested login failed for user {0}.", Rzrlf.SP.Innovator.WebParts.Settings.InnovatorUserName));
            }

            invtr = Aras.IOM.IomFactory.CreateInnovator(con);

            return invtr;
        }

        #endregion
    }
}
